Print All Links of a Webpages
Use your Package Name and Class Name
package asc; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import io.github.bonigarcia.wdm.WebDriverManager; public class printAllLink { public static void main(String[] args) { WebDriverManager.chromedriver().setup(); ChromeDriver driver=new ChromeDriver(); driver.get("https://www.sugarcrm.com/au/request-demo/"); driver.manage().window().maximize(); Listalltags = driver.findElements(By.tagName("a")); System.out.println("Total tags are: "+alltags.size()); for(int i=0;i < alltags.size();i++) { System.out.println("Link on page are "+alltags.get(i).getAttribute("href")); System.out.println("Link on page are "+alltags.get(i).getText()); } } }


Code Explanation:
1. Pacakage And Imports:
package asc;:
Declares the package name.- Necessary Selenium and WebDriverManager classes are imported.
2. Class and Main Method:
- The class printAllLink contains the main method, which is the entry point of the program.
3. WebDriverManager and ChromeDriver Setup:
WebDriverManager.chromedriver().setup();:
This line automatically handles the setup of the ChromeDriver binary, making it easier to work with different versions of Chrome.ChromeDriver driver = new ChromeDriver();:
Initializes a new ChromeDriver instance to control the Chrome browser.
4. Navigating to the Web Page:
driver.get("https://www.sugarcrm.com/au/request-demo/");:
Opens the specified URL in the Chrome browser.driver.manage().window().maximize();:
Maximizes the browser window.
5. Finding and Printing Links:
List
Finds all elements with the a tag (links) on the page and stores them in a list.alltags = driver.findElements(By.tagName("a"));: System.out.println("Total tags are: " + alltags.size());:
Prints the total number of a tags found on the page.- The for loop iterates over each link, printing both the href attribute (URL) and the visible text of the link.
getAttribute("href"):
Gets the URL that the link points to.getText():
Gets the visible text of the link.
Option Tags
Code
package asc; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import io.github.bonigarcia.wdm.WebDriverManager; public class printAllLink { public static void main(String[] args) { WebDriverManager.chromedriver().setup(); ChromeDriver driver=new ChromeDriver(); driver.get("https://www.salesforce.com/in/form/demo/starter-overview-demo/?d=jumbo2-btn-demo"); driver.manage().window().maximize(); Listalltags = driver.findElements(By.tagName("option")); System.out.println("Total tags are: "+alltags.size()); for(int i=0;i < alltags.size();i++) { System.out.println("Link on page are "+alltags.get(i).getAttribute("value")); } } }



4. Navigating to the Web Page:
driver.get("https://www.salesforce.com/in/form/demo/starter-overview-demo/?d=jumbo2-btn-demo");:
This navigates to the Salesforce demo request page.driver.manage().window().maximize();:
Maximizes the browser window for better visibility and interaction.
5. Finding and Printing option Tags:
List
Finds all elements with the option tag on the page, which are typically used within < select> dropdown elements.alltags = driver.findElements(By.tagName("option"));: System.out.println("Total tags are: " + alltags.size());:
Prints the total number of option tags found.- A for loop iterates through the list of option elements, printing the value attribute of each option tag.
getAttribute("value"):
Retrieves the value attribute from the option tag, which often represents the value sent to the server when the form is submitted.